//
//  GQHMessageController.m
//  Xanthium
//
//  Created by GuanQinghao on 07/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHMessageModel.h"

#pragma mark Object

#pragma mark View
#import "GQHMessageView.h"

#pragma mark Controller
#import "GQHMessageController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHMessageController () {
    
#pragma mark Data
    NSMutableDictionary *parameters;
    
    NSMutableArray<GQHMessageModel *> *messageModelArray;
#pragma mark View
    /// 自定义根视图
    GQHMessageView *rootView;
}

@end


@implementation GQHMessageController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHMessageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    
    if ([_qh_messageType isEqualToString:@"01"]) {
        
        [parameters setObject:@"01" forKey:@"newsCategory"];
    } else if ([_qh_messageType isEqualToString:@"02"]) {
        
        [parameters setObject:@"02" forKey:@"newsCategory"];
    } else if ([_qh_messageType isEqualToString:@"03"]) {
        
        [parameters setObject:@"03" forKey:@"newsCategory"];
    } else {
        
        [parameters setObject:@"04" forKey:@"newsCategory"];
    }
    
    [self fetchMessageWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    if ([_qh_messageType isEqualToString:@"01"]) {
        
        self.qh_titleLabel.text = @"竞价消息";
    } else if ([_qh_messageType isEqualToString:@"02"]) {
        
        self.qh_titleLabel.text = @"运单消息";
    } else if ([_qh_messageType isEqualToString:@"03"]) {
        
        self.qh_titleLabel.text = @"系统消息";
    } else {
        
        self.qh_titleLabel.text = @"推送消息";
    }
    
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
    [rootView.qh_tableView.mj_header endRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchMessageWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHMessageModel qh_fetchMessagesWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHMessageModel *> *messages) {
            DLog(@"%@--%@--%@",code,message,messages);
            
            if (status) {
                
                messageModelArray = [NSMutableArray arrayWithArray:messages];
                rootView.qh_messageArray = messageModelArray;
                [rootView.qh_tableView reloadData];
                
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHMessageModel qh_fetchMessagesWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHMessageModel *> *messages) {
            
            if (status) {
                
                if (messages && messages.count > 0) {
                    
                    [messageModelArray addObjectsFromArray:messages];
                    rootView.qh_messageArray = messageModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
